var userCouponList = server_host + '/uweb/coupon/userCouponList.do';

var appModule = angular.module('CouponList', []);
appModule.controller('CouponCtrl', ['$scope', '$http', function($scope, $http){

	$scope.loadList = function(loadedCallback){
		var URL = '';
		if($scope.listType == 0){
			URL = userCouponList + '?useType=' + 0;
			$http.get(URL).success(function(data, status, headers, config){
				console.log(data);
				$scope.avaliableCouponList = data.data;

				var obj = $scope.avaliableCouponList;
				$scope.arr = [];
				for ( property in obj ) {
				  $scope.arr.push({brandName:property,coupons:obj[property]});
				}
				$scope.couponsArr = $scope.arr[0];
			});
		}else{
			URL = userCouponList + '?useType=' + 1;
			$http.get(URL).success(function(data, status, headers, config){
				console.log(data);
				$scope.historyCouponList = data.data;

				var objHsty = $scope.historyCouponList;
				$scope.arrHsty = [];
				for ( property in objHsty ) {
				  $scope.arrHsty.push({brandName:property,coupons:objHsty[property]});
				}
				$scope.couponsArrHsty = $scope.arrHsty[0];

				if (typeof loadedCallback == 'function') {
					loadedCallback();
				}
			});
		}
	}

	// 选品牌(可用红包券)
	$scope.brandChoise = function(index){
		$scope.couponsArr = $scope.arr[index];
		$('#choise').modal('hide');
	}

	// 选品牌(历史红包)
	$scope.hstyBrandChoise = function(index){
		$scope.couponsArrHsty = $scope.arrHsty[index];
		$('#history-choise').modal('hide');
	}

	// 选项卡
	$scope.switchType = function(typeIndex){
		$scope.listType = typeIndex;
	}

	// 红包说明
	$scope.illustrateCoupon = function(index){
		var couponId = $scope.couponsArr.coupons[index].couponId;
		location.href = 'coupon-illustrate.html?couponId=' + couponId;
	}

	$scope.loadList(function(){
		$scope.listType = 0;
		$scope.loadList();
	})

}]);

// 店铺选择小箭头js
// $('.close-modal').click(function() {
// 	if ($('#choise').is(":hidden")) {
// 		alert(0);
// 		$('.close-modal').addClass("open-modal");
// 	} else {
// 		$('.close-modal').removeClass("open-modal");
// 	};
// });

// if ($('#choise').is(":hidden")) {
// 	$('.close-modal').click(function(){
// 		$('.close-modal').addClass("open-modal");
// 	});
// }else{
// 		$('.close-modal').removeClass("open-modal");
// }